package com.ruimei.service.impl;

import com.ruimei.entitys.LeaveTeacher;
import com.ruimei.entitys.Teacher;
import com.ruimei.entitys.enums.LeaveStatus;
import com.ruimei.mapper.TeacherLeaveMapper;
import com.ruimei.service.TeacherLeaveService;
import com.ruimei.util.CommUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherLeaveServiceImpl implements TeacherLeaveService {
    @Resource
    TeacherLeaveMapper mapper;

    @Override
    public Map<String, Object> getDept() {
        Map<String, Object> map = new HashMap();
        List<Teacher> list = mapper.getDept();
        map.put("status", Boolean.valueOf(true));
        map.put("message", "查询成功");
        map.put("count", Integer.valueOf(list.size()));
        map.put("data", list);
        return map;
    }

    @Override
    public Map<String, Object> getTeacher(String deptId) {
        Map<String, Object> map = new HashMap();

        List<Teacher> list = mapper.getTeacherByDeptId(deptId);

        map.put("status", Boolean.valueOf(true));
        map.put("message", "查询成功");
        map.put("count", Integer.valueOf(list.size()));
        map.put("data", list);
        return map;
    }

    @Override
    public Map<String, Object> addTeacherLeave(LeaveTeacher leaveTeacher) {
        Map<String, Object> map = new HashMap();
        List<Teacher> teacherList = mapper.getTeacher(leaveTeacher.getTeacherId());
        Teacher teacher = teacherList.get(0);

        leaveTeacher.setDeptName(teacher.getDeptName());
        leaveTeacher.setDeptId(teacher.getDeptId());
        leaveTeacher.setTeacherName(teacher.getFullName());
        leaveTeacher.setStatus(LeaveStatus.PROCESSING);
        Integer i = mapper.addTeacherLeave(leaveTeacher);

        if(i.intValue() == 1){
            map.put("status", Boolean.valueOf(true));
            map.put("message", "添加成功");
        }else{
            map.put("status", Boolean.valueOf(false));
            map.put("message", "添加失败");
        }

        return map;

    }

    @Override
    public Map<String, Object> getTeacherLeave(LeaveTeacher leaveTeacher) {
        Map<String, Object> map = new HashMap();

        List<LeaveTeacher> leaves = mapper.getTeacherLeave(leaveTeacher);

        List<LeaveTeacher> list = TimeOut(leaves); //验证超时

        map.put("status", Boolean.valueOf(true));
        map.put("message", "查询成功");
        map.put("count", Integer.valueOf(list.size()));
        map.put("data", list);
        return map;
    }

    public List<LeaveTeacher> TimeOut(List<LeaveTeacher> leaves) { //验证时间是否超时
        int index = 0;
        for (LeaveTeacher leave : leaves) {
            Double nowTime = Double.valueOf(CommUtil.getStringDate().replaceAll("[-\\s:]", ""));
            Double endTime = Double.valueOf(leave.getLeaveEndTime().replaceAll("[-\\s:]", ""));
            if ((nowTime.doubleValue() >= endTime.doubleValue()) && (leave.getStatus() == LeaveStatus.PROCESSING)) {

                leave.setStatus(LeaveStatus.TIMEOUT);
                mapper.setTeacherLeave(leave);

                leaves.set(index,mapper.getTeacherLeave(leave).get(0));
            }
            index++;
        }
        return leaves;
    }

    @Override
    public Map<String, Object> setTeacherLeave(LeaveTeacher leaveTeacher) {
        Map<String, Object> map = new HashMap();

        Integer i = mapper.setTeacherLeave(leaveTeacher);

        if(i.intValue() == 1){
            map.put("status", Boolean.valueOf(true));
            map.put("message", "修改成功");
        }else{
            map.put("status", Boolean.valueOf(false));
            map.put("message", "添加失败");
        }

        return map;
    }
}
